package com.aidong.stadium.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.jobs.async.SmsTask;
import com.aidong.stadium.biz.MstCoachBiz;
import com.aidong.stadium.entity.MstCoach;
import com.aidong.util.CalendarUtil;
import com.aidong.util.MyPersonalInfoUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.aidong.util.bus.SecurityUtils;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 私教预约action
 * 
 * @author java
 * @version 1.0 2015.6.29
 */
@ParentPackage("json")
@Namespace("/")
public class MstCoachAction implements ModelDriven<MstCoach> {

	private MstCoach coach; // 私教实体类1

	@Override
	public MstCoach getModel() {
		if (coach == null) {
			coach = new MstCoach();
		}
		return coach;
	}

	private MstCoachBiz mstCoachBiz; // 私教预约service

	private MstCampaignBiz mstCampaignBiz; // 活动service

	@Autowired
	public void setMstCoachBiz(MstCoachBiz mstCoachBiz) {
		this.mstCoachBiz = mstCoachBiz;
	}

	/**
	 * 活动service
	 * 
	 * @param mstCampaignBiz
	 */
	@Autowired
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	}

	@Autowired
	SmsTask smsTask;

	Map<String, Object> map;

	/**
	 * map
	 * 
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	/**
	 * 私教排班表及介绍
	 * 
	 * @return
	 */
	@Action(value = "getmstcoachdetail", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMstCoachDetail() {
		map = new HashMap<String, Object>();
		try {
			int coachId = coach.getCoachId(); // 私教ID
			int idongId = coach.getIdongId(); // 爱动号
			Map<String, Object> resultMap = new HashMap<String, Object>();
			if (coachId > 0) {
				// 教练介绍
				Map<String, Object> coachMap = mstCoachBiz.getMstCoachDetail(coachId);
				coachMap.put("photoUrl", ReadPropertiesUtil.getIMGREAD(coachMap.get("photoUrl")));
				coachMap.put("largePhoto", ReadPropertiesUtil.getIMGREAD(coachMap.get("largePhoto")));

				// 关注状态
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId); // 关注人
				paraMap.put("tionIdongId", coachMap.get("idongId")); // 被关注人
				int attionCount = mstCampaignBiz.getIfAttention(paraMap);
				if (idongId > 0) {
					if (attionCount > 0) { // 已关注
						coachMap.put("attionStatus", 1);
					} else { // 关注
						coachMap.put("attionStatus", 2);
					}
				} else { // 关注
					coachMap.put("attionStatus", 2);
				}
				resultMap.put("coachMsg", coachMap);

				// 私教预约时间排班表
				List<Map<String, Object>> dateMap = CalendarUtil.getCalenderWeek();
				// 私教不可预约时间排班表
				List<Map<String, Object>> unDateMap = mstCoachBiz.getTrnPtUnsubscribe(coachId);

				// 不可预约状态
				String sysYear = new SimpleDateFormat("yyyy").format(new Date()); // 系统当前年份
				// 没有不可预约时间
				if (unDateMap.size() == 0) {
					for (Map<String, Object> onday : dateMap) {
						onday.put("appointStatus", 1); // 可预约
					}
				} else {
					SimpleDateFormat sim = new SimpleDateFormat("M月dd日");// 不可预约日期格式
					for (Map<String, Object> onday : dateMap) {
						String SubDate = onday.get("dateDay").toString(); // 私教预约排班表(日期)
						for (Map<String, Object> unday : unDateMap) {
							if (unday.get("unSubYear").toString().equals(sysYear)) {
								Date unDaye = sim.parse(unday.get("unSubDate").toString()); // 不可预约日期
								if (sim.format(unDaye).equals(SubDate)) {
									onday.put("appointStatus", 0); // 0、不可预约
																	// 1、可预约
									break;
								} else {
									onday.put("appointStatus", 1);
									break;
								}
							} else {
								onday.put("appointStatus", 1);
								break;
							}
						}
					}
				}
				resultMap.put("appointDate", dateMap);

				map.put("code", 1);
				map.put("result", resultMap);
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
		return "succ";
	}

	/**
	 * 添加私教预约信息
	 * 
	 * @return
	 */
	@Action(value = "addptappoint", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String addPtAppoint() {
		map = new HashMap<String, Object>();
		try {
			int coachId = coach.getCoachId(); // 私教ID
			int storeId = coach.getStoreId(); // 门店ID
			// 解密爱动号
			coach.setIdongId(SecurityUtils.decryptIdongId(coach.getIdongIdEncrypt())); // 爱动号
			int idongId = coach.getIdongId(); // 爱动号
			int pmAm = coach.getPmAm(); // 预约上午/下午 1、上午 2、下午
			String shangWu = MyPersonalInfoUtil.dateConversion(pmAm);
			String contactName = coach.getContactName(); // 联系人姓名

			// 解密联系人电话
			coach.setContactPhone(SecurityUtils.decryptPhoneNo(coach.getContactPhoneEncrypt()));
			String contactPhone = coach.getContactPhone(); // 联系人电话
			String stratDate = coach.getStartDate(); // 预约日期
			if (coachId > 0 && storeId > 0 && pmAm > 0 && (!"".equals(contactName) && contactName != null)
					&& (!"".equals(contactPhone) && contactPhone != null)
					&& (!"".equals(stratDate) && stratDate != null)) {

				stratDate = CalendarUtil.dateFomat(stratDate); // 预约日期
				coach.setStartDate(stratDate);

				// Map<String, Object> paraMap = new HashMap<String, Object>();
				// paraMap.put("coachId", coachId);
				// paraMap.put("startDate", stratDate);
				// paraMap.put("pmAm", pmAm);
				// 查看该私教是否已被预约
				// int appointCount = mstCoachBiz.getIfAppointPt(paraMap);
				// if(appointCount > 0){ // 已预约
				// map.put("code", 0);
				// map.put("msg", "该私教已经被约了");
				// return "succ";
				// }else{ // 未预约

				// }

				// 私教信息，短信预约提醒信息
				Map<String, String> coachMap = mstCoachBiz.getCoachAppointRemind(coachId);
				String coachName = coachMap.get("chName"); // 私教中文名
				String coachEnName = coachMap.get("enName"); // 私教英文名
				String remindDate = stratDate + " " + shangWu; // 提醒预约时间
				String address = coachMap.get("address"); // 地址

				if (idongId > 0) { // 已登录
					// 直接保存预约信息
					mstCoachBiz.addTrnPtUnsubscribe(coach);
					// 私教预约短信提醒
					smsTask.ptAppointment(contactPhone, coachName, coachEnName, remindDate, address,
							coach.getSubscribeId());
					map.put("code", 1);
					map.put("result", "预约成功");
					return "succ";
				} else { // 未登录
							// 是否已注册
					int count = mstCampaignBiz.getIfRegister(contactPhone);
					if (count > 0) { // 已注册
						// 直接保存预约信息
						int rsultIdongId = mstCampaignBiz.getIdongIdByPhone(contactPhone);
						coach.setIdongId(rsultIdongId);
						mstCoachBiz.addTrnPtUnsubscribe(coach);
						smsTask.ptAppointment(contactPhone, coachName, coachEnName, remindDate, address,
								coach.getSubscribeId());
						map.put("code", 1);
						map.put("result", "预约成功");
						return "succ";
					} else { // 未注册
								// 先注册新用户，产生爱动号
						Map<String, Object> regMap = new HashMap<String, Object>();
						regMap.put("contactName", contactName);
						regMap.put("contactPhone", contactPhone);
						mstCampaignBiz.addMemberReg(regMap);
						// 注册成功后产生爱动号
						int regIdongId = Integer.parseInt(regMap.get("idongId").toString());
						coach.setIdongId(regIdongId);
						// 保存预约信息
						mstCoachBiz.addTrnPtUnsubscribe(coach);
						/*
						 * SMSUtil.pool.execute(new Runnable() { public void
						 * run() {
						 */
						try {
							// 私教预约短信提醒
							smsTask.ptAppointment(contactPhone, coachName, coachEnName, remindDate, address,
									coach.getSubscribeId());
						} catch (Exception e) {
							e.printStackTrace();
						}
						/*
						 * } });
						 */
						map.put("code", 1);
						map.put("result", "预约成功");
						return "succ";
					}
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
